/******************************************************************
 *	Private properties and methods
 ******************************************************************/
//The User object
var User = function(options){
	//private properties
	var name = options.name;
	var password = options.password;
	//public property
	this.user = options.user;
	
	//private method
	function privateMethod(){
		console.debug('im a private method!')
	}
}

//new User instance
var u = new User({name:'Crysfel',password:'***',user:'cvilla'});

console.debug(u.user); //cvilla
console.debug(u.name); //undefined
//console.debug(u.privateMethod()); //throw error

/******************************************************************
 *	Privileged methods
 ******************************************************************/
 
 var Person = function(options){
	//private properties
	var name = options.name
	var birthYear = options.birthYear;
	//private method
	var calculateAge = function(){
		var today = new Date();
		return today.getFullYear() - birthYear;
	}
	//Privileged method
	this.getAge = function(){
		return calculateAge(); //calling private method
	}
 }
 
//new Person  instance
var p = new Person({name:'Peter',birthYear:1983});
 
console.debug(p.getAge());// the age
console.debug(p.name);// undefined
console.debug(p.birthYear);// undefined
 
/******************************************************************
 *	Dinamic methods
 ******************************************************************/
 
var Animal = function(options){
	//a private method to do the job 
	//and to create a new scope (see last chapter)
	function createGetterAndSetters(properties,property){
		//attach the getter for the current property
		this['get'+property] = function(){
			return properties[property];
		}
		//attach the setter for the current property
		this['set'+property] = function(value){
			properties[property] = value;
		}
	}
	//iterate through the options
	for(var property in options){
		//call the private method with the right context
		createGetterAndSetters.call(this,options,property);
	}
}

//two diferent instances of Animal object
var zul = new Animal({breed:'Chihuahua',gender:'female',name:'Zul'});
var rocky = new Animal({breed:'Beagle',gender:'male',name:'Rocky'});

console.debug(zul.getbreed());//Chihuahua
console.debug(zul.getname());//Zul
console.debug(zul.name);//undefined
console.debug(rocky.getbreed());//Beagle
console.debug(rocky.getgender());//male
console.debug(rocky.getname());//Rocky
console.debug(rocky.breed);//undefined